<?php
declare(strict_types=1);

namespace app\common\controller;

use think\Auth;
use think\facade\Config;
use think\facade\Session;
use think\facade\View;

/**
 * | Notes：Admin
 * +----------------------------------------------------------------------
 * | PHP Version 7.2+
 * +----------------------------------------------------------------------
 * | Copyright (c) 2011-2020 https://www.xxq.com.cn, All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 和好 <389123492@qq.com>
 * +----------------------------------------------------------------------
 * | Date: 2020/5/20 10:35
 * +----------------------------------------------------------------------
 */
class Admin extends AppBase
{
    /**
     * initialize 初始化
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        // 网关入口
        $redirect_url = strtolower(Config::get('auth.admin_gateway'));

        if (is_login($this->module_name)) {
            $auth_obj  = new Auth();
            $auth_list = fn_array_filter($auth_obj->getAuthList(session($this->module_name . '_login.uuid'), 1, 'create_time, update_time'));

            $filter_auth_list = fn_array_filter($auth_list);

            $menu = list_to_tree($filter_auth_list);

            //halt($menu);

            $term_validity      = Session::get($this->module_name . '_login.login_time') + 60000;
            $ineffective_period = $term_validity - time();

            if ($ineffective_period <= 60) {
                // 还没登录 跳转到登录页面
                header('Location: ' . url($redirect_url));
                exit;
            } else {
                // 系统信息
                View::assign([
                    'session'  => Session::get($this->module_name . '_login'),
                    'out_time' => $ineffective_period,
                    'menu'     => $menu
                ]);
            }
        } else {
            // 还没登录 跳转到登录页面
            header('Location: ' . url($redirect_url));
            exit;
        }
    }
}
